package com.brook.app.android.filepicker.core;

import android.webkit.MimeTypeMap;

import androidx.core.content.MimeTypeFilter;

import com.brook.app.android.filepicker.cache.FilePickerCacheDao;
import com.brook.app.android.filepicker.cache.FilePickerDatabase;
import com.brook.app.android.filepicker.cache.entity.ImageFileItemEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author Brook
 * @time 2020-03-14 13:59
 */
public abstract class DefaultFileProvider implements IFileProvider {

    protected FilePickerConfig mFilePickerConfig;

    @Override
    public void injectConfig(FilePickerConfig config) {
        this.mFilePickerConfig = config;
    }

    @Override
    public boolean handleMimeType(String mimeType) {
        return MimeType.ALL.toString().equals(mimeType);
    }

    @Override
    public boolean handlerFile(File file) {
        String fileName = file.getName().toLowerCase();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        return MimeTypeFilter.matches(mFilePickerConfig.getPickerMimeType(), MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix));
    }

    @Override
    public void getAllFile(FilePickCallback callback) {
        FilePickerCacheDao database = FilePickerDatabase.getInstance();
        List<ImageFileItemEntity> allImage = database.getAllImage();
        List<File> allFile;
        if (allImage == null || allImage.isEmpty()) {
            allFile = getAllFile();
        } else {
            allFile = new ArrayList<>(allImage.size());
            for (ImageFileItemEntity imageFileItemEntity : allImage) {
                File file = new File(imageFileItemEntity.filePath);
                if (file.exists()) {
                    allFile.add(file);
                }
            }
        }
        callback.onPickResult(allFile);
    }

    @Override
    public void getAllFolder(final FilePickCallback callback) {
        getAllFile(new FilePickCallback() {
            @Override
            public void onPickResult(List<File> files) {
                Set<File> parentDirFile = new LinkedHashSet<>();
                for (File file : files) {
                    parentDirFile.add(file.getParentFile());
                }
                callback.onPickResult(new ArrayList<>(parentDirFile));
            }
        });
    }


    @Override
    public void getOrderGroupFile(final File parentFile, final FilePickCallback callback) {
        new Thread() {
            @Override
            public void run() {
                LinkedList<File> resultList = new LinkedList<>();
                getOrderGroupFile(parentFile, resultList);
                callback.onPickResult(resultList);
            }
        }.start();
    }

    private void getOrderGroupFile(File parentFile, List<File> resultList) {
        File noMediaFile = new File(parentFile, ".nomedia");
        if (noMediaFile.exists()) {
            // 不扫描nomedia文件夹
            return;
        }

        File[] files = parentFile.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    getOrderGroupFile(file, resultList);
                } else {
                    if (handlerFile(file)) {
                        resultList.add(file);
                    }
                }
            }
        }
    }

    protected abstract List<File> getAllFile();
}
